# CCNv2::Provision::Base.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

package CCNv2::Provision::Base;
use strict; use warnings;
use base qw(CCNCore::Provision::Base);

sub enterprise_salt
{
    my $self = shift;
    my $enterprise = shift;
    $self->{enterprise_salt}->{$enterprise} = $self->sql_get( q(SELECT salt FROM enterprise WHERE system = ? AND pk = ?), $self->p->system_pk, $enterprise )
        if not exists $self->{enterprise_salt} or not exists $self->{enterprise_salt}->{$enterprise};
    return $self->{enterprise_salt}->{$enterprise};
}


# ast_route_trunk_inbound($trunk): inbound call routing for a trunk (zaptrunk or siptrunk)
sub ast_route_trunk_inbound($)
{
    my $self = shift;
    my $TRUNK = shift;
    my $routeconf = '';
    # Inbound extensions.conf
    # The flow is: trunk => "from-gateway-{trunk}" => rewrite => "route-from-trunk-{trunk}" => assign => "{enterprise}-inbound-{trunk}".)
    $routeconf .= "[from-gateway-${TRUNK}]\n";
    # rewrite
    $self->sql_do_for( <<'SQL', $TRUNK,
        SELECT pattern, pattern_is_prefix, keep, skip, prepend FROM trunk_prerouting
        WHERE trunk = ?
SQL
        sub
        {
            my ($num,$pattern_is_prefix,$keep,$skip,$prepend) = @_;
            $keep ||= 0;
            $skip ||= 0;
            my $remove = length($num)-$keep+$skip;
            $num .= '.' if $pattern_is_prefix;
            $routeconf .=
            "exten => _${num},1,Goto(route-from-trunk-${TRUNK},$prepend\${EXTEN:${remove}},1)\n";
        });

    $routeconf .= "[route-from-trunk-${TRUNK}]\n";
    # assign
    $self->sql_do_for( <<'SQL', $TRUNK,
        SELECT TR.pattern, TR.pattern_is_prefix, P.enterprise, TR.offnet, TR.pool 
        FROM trunk_routing TR, pool P
        WHERE TR.trunk = ? AND TR.pool = P.pk
SQL
        sub
        {
            my ($route,$pattern_is_prefix,$target,$offnet,$pool) = @_;
            $offnet = ($offnet =~ m/^t/i) ? '1' : '0';
            $route .= '.' if $pattern_is_prefix;
            $routeconf .=
            "exten => _${route},1,Set(__ROUTE=${route})\n".
            "exten => _${route},n,Set(__ENTERPRISE=${target})\n".
            "exten => _${route},n,Set(CDR(userfield)=${TRUNK})\n".
            "exten => _${route},n,Set(__OFFNET=${offnet})\n".
            "exten => _${route},n,Set(__POOL=${pool})\n".
            # Direct to the CoS defined in entcoslist.
            "exten => _${route},n,Goto(${target}-cos-inbound-${TRUNK},\${EXTEN},1)\n";
        });

    return $routeconf;
}

use CCNv2::pstn_recipes;

# ast_route_trunk_outbound($trunk): outbound call routing for a trunk (zaptrunk or siptrunk)
sub ast_route_trunk_outbound($)
{
    my $self = shift;
    my $TRUNK = shift;
    my $routeconf = '';

    my ($COUNTRY,$PSTNLOCAL,$NPA) = $self->sql_get('SELECT country, pstnlocal, npa FROM trunk WHERE pk = ?', $TRUNK);

    # cf asterisk_outbound for the sending end of this.
    $routeconf .= "[route-to-trunk-${TRUNK}]\n";
    return $routeconf . "exten => _X.,1,Goto(dial-trunk-${TRUNK},\${EXTEN},1)\n"
        if not defined $COUNTRY;

    my $recipe = $CCNv2::pstn_recipes::e164_to_pstn_recipes{$COUNTRY};

    return $self->error("Unknown or unhandled country: $COUNTRY.")
        unless defined $recipe;

    for my $do ( keys %{$recipe} )
    {
        my $how = $recipe->{$do}->($PSTNLOCAL,$NPA);
        my ($pattern,$skip,$keep,$prepend) = @{$how};
        $pattern = "_$pattern" unless $pattern eq 'emergency';
        $routeconf .= "exten => ${pattern},1,Goto(dial-trunk-${TRUNK},${prepend}\${EXTEN:${skip}:${keep}},1)'\n";
    }
    return $routeconf;
}

1;